package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.SQLException;

public class TeacherQueryStudentInfoController implements TeacherSubController {

    @FXML
    private TextField studentIdField;
    @FXML
    private VBox studentInfoDisplay;
    @FXML
    private Label studentIdLabel;
    @FXML
    private Label studentNameLabel;
    @FXML
    private Label studentTypeLabel;
    @FXML
    private Label studentDepartmentLabel;
    @FXML
    private Label studentStatusLabel;

    private Stage primaryStage;
    private TeacherController teacherController;
    private User currentUser;
    private UserDAO userDAO = new UserDAO();

    @FXML
    public void initialize() {
        // Initialize the display area to be empty or show a default message
        clearStudentInfo();
    }

    @FXML
    private void handleQuery() {
        String studentId = studentIdField.getText();
        if (studentId == null || studentId.trim().isEmpty()) {
            // Optionally show a warning
            clearStudentInfo();
            return;
        }

        try {
            User user = userDAO.getUserByUsername(studentId.trim());

            if (user != null && "student".equals(user.getType())) {
                // Display student information
                studentIdLabel.setText("ID: " + user.getUsername());
                studentNameLabel.setText("姓名: " + user.getName());
                studentTypeLabel.setText("用户类型: " + user.getType());
                studentDepartmentLabel.setText("院系: " + user.getDepartment());
                studentStatusLabel.setText("状态: " + user.getStatus());
            } else {
                // Student not found or is not a student type
                clearStudentInfo();
                // Optionally show a message to the user
            }

        } catch (SQLException e) {
            e.printStackTrace();
            // Optionally show an error message to the user
            clearStudentInfo();
        }
    }

    private void clearStudentInfo() {
        studentIdLabel.setText("ID:");
        studentNameLabel.setText("姓名:");
        studentTypeLabel.setText("用户类型:");
        studentDepartmentLabel.setText("院系:");
        studentStatusLabel.setText("状态:");
    }

    @Override
    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    @Override
    public void setTeacherController(TeacherController teacherController) {
        this.teacherController = teacherController;
    }

    @Override
    public void setCurrentUser(User user) {
        this.currentUser = user;
        // Use currentUser if needed, though not strictly necessary for this function
    }
} 